/**
 * @author lteng
 * @time 2022-06-17 19:14:38 
 */
 
package liteng.study.cn.stuscore.controller;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import liteng.study.cn.stuscore.comm.Page;
import liteng.study.cn.stuscore.entity.EType;
import liteng.study.cn.stuscore.service.ETypeService;
import liteng.study.cn.stuscore.service.impl.ETypeServiceImpl;

@WebServlet("/type")
public class ETypeServlet extends HttpServlet {
	private ETypeService eTypeService = new ETypeServiceImpl();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("type-get方法已启动");
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");

		if (action.equals("info")) {
			descInfo(request, response);

		} else if (action.equals("page")) {
			pageInfo(request, response);

		} else if (action.equals("add")) {
			addInfo(request, response);

		} else if (action.equals("upd")) {
			updInfo(request, response);

		} else if (action.equals("del")) {
			delInfo(request, response);
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("type-post方法已启动");

		doGet(request, response);
		
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	private void delInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		int id = Integer.valueOf(request.getParameter("id"));

		EType eType = eTypeService.getOne(id);

		eTypeService.delete(eType);

		response.sendRedirect("/stuscore/type?action=page&pageIndex=1&pageSize=10");

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void updInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		String comm = request.getParameter("comm");
		int id = Integer.valueOf(request.getParameter("id"));

		EType eType = eTypeService.getOne(id);

		if (!name.equals(eType.getName()) && isExist(name)) {
			request.setAttribute("error", "要修改的信息已存在！");

			request.getRequestDispatcher("type?action=info&id="+id).forward(request, response);
		} else {
			eType.setName(name);
			eType.setComm(comm);

			eTypeService.update(eType);

			response.sendRedirect("/stuscore/type?action=page&pageIndex=1&pageSize=10");
		}
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void pageInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int pageIndex = Integer.valueOf(request.getParameter("pageIndex"));
		int pageSize = Integer.valueOf(request.getParameter("pageSize"));
//		String name = request.getParameter("name");
		
		String name = request.getParameter("name");
		if (name!=null) {
			name = URLDecoder.decode(name, "utf-8");
		}
		
		Page page = null;
		if (name != null && !name.equals("")) {
			page = eTypeService.getPageETypes(pageIndex, pageSize, name);
			
		} else {
			page = eTypeService.getPageETypes(pageIndex, pageSize);


		}
		request.setAttribute("page", page);

		request.getRequestDispatcher("/pages/type/list.jsp").forward(request, response);

	}

	/**
	 * 修改年级信息
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void descInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		int id = Integer.valueOf(request.getParameter("id"));
		
		EType eType = eTypeService.getOne(id);
		request.setAttribute("type", eType);
		request.getRequestDispatcher("/pages/type/upd.jsp").forward(request, response);

	}

	

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String comm = request.getParameter("comm");

		if (isExist(name)) {
			request.setAttribute("error", "要添加的信息已存在");
			request.getRequestDispatcher("/pages/type/add.jsp").forward(request, response);

		} else {
			EType eType = new EType();
			eType.setName(name);
			eType.setComm(comm);

			eTypeService.add(eType);

			response.sendRedirect("/stuscore/type?action=page&pageIndex=1&pageSize=10");
		}
	}

	/**
	 * 
	 * @param name
	 * @return
	 */
	private boolean isExist(String name) {
		// TODO Auto-generated method stub
		return eTypeService.isExistEType(name);
	}

}
